﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.BaseLibrary.Tools.PageTools;
using SystemCenterSoa.DtoModel.ContractManager;
using SystemCenterSoa.IRepository.SystemManager;
using SystemCenterSoa.PoModel;
using SystemCenterSoa.PoModel.SystemManager;
using SystemCenterSoa.PoModel.SystemManager.MyModelPo;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.SystemManager
{
    public class StructualRepository : BaseRepository, IStructural
    {
        public StructualRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {

        }
        public List<Structural_style> GetStructuralTree()
        {
            return GetStructuralTreeList(_systemCenterDbSoaContext.Structural_style.ToList(), 0);
        }

        public List<Build_TypePo> GetBuildTree()
        {
            return BuildTreeList(_systemCenterDbSoaContext.Build_TypePo.ToList(), 0);
        }

        private List<Build_TypePo> BuildTreeList(List<Build_TypePo> db, int id)
        {
            return db.Where(a => a.build_type_parentid == id).Select(t => new Build_TypePo
            {
                build_type_id = t.build_type_id,
                build_type_parentid = t.build_type_parentid,
                build_type_name = t.build_type_name,
                Build_Type_Children = BuildTreeList(db, t.build_type_id).Count() == 0 ? null : BuildTreeList(db, t.build_type_id)

            }).ToList();
        }

        private List<Structural_style> GetStructuralTreeList(List<Structural_style> db, int id)
        {
            return db.Where(a => a.structural_parentid == id).Select(t => new Structural_style
            {
                structural_id = t.structural_id,
                structural_parentid = t.structural_parentid,
                structural_name = t.structural_name,
                structural_children = GetStructuralTreeList(db, t.structural_id).Count() == 0 ? null : GetStructuralTreeList(db, t.structural_id)

            }).ToList();
        }

        public List<DictionariesPo> GetDictionaryByType_key()
        {
            return _systemCenterDbSoaContext.Dictionariess.Where(a => a.dic_type_key == "Contract_Type").ToList();

        }

        public bool ContractAdd(ContractPo po)
        {
            _systemCenterDbSoaContext.ContractPo.Add(po);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        //public List<PageList<ContractPo>> GetContractPage(int  DepartmentId, string project_name, int contract_type, int construction_unit, DateTime ContractDate, string CustomerName, DateTime MinCreateTime, DateTime MaxCreateTime, string CustomerAccount, string linkman)
        //{
        //    var query = _systemCenterDbSoaContext.ContractPo.AsEnumerable();

        //    if (DepartmentId != 0)
        //    {
        //        query=query.Where(a=>a.)
        //    }
            
        //}
    }
}
